#include "Game/Tilemaps/TurretTilemap.h"

namespace TD_Game {

	/************************************************************************/
	/* Protected functions                                                  */
	/************************************************************************/

	Tile * TurretTilemap::createNewTile(unsigned int i, unsigned int j, int relativePosition, int relativeTilesetPosition) {
		Tile *nuTile = new Tile();
		*nuTile = *m_pTileset->getTile(relativeTilesetPosition);
		nuTile->setPosition(getTileposition(i, j, relativePosition));
		return nuTile;
	}


	/************************************************************************/
	/* ctors and dtors                                                      */
	/************************************************************************/
	
	TurretTilemap::TurretTilemap() {

	}
	TurretTilemap::TurretTilemap(const Tileset *g_pTileset) : Tilemap(g_pTileset) {

	}

	TurretTilemap::TurretTilemap(const TurretTilemap &rhs) : Tilemap(rhs) {
		
	}

	TurretTilemap::~TurretTilemap() {

	}

	/************************************************************************/
	/* Functionalities                                                      */
	/************************************************************************/
	bool TurretTilemap::loadTilemap(const Tileset *g_pTileset, const string &tileMapStr, const Coord2D & mapPosition, const Coord2D &numTiles) {
		if(g_pTileset == NULL) {
			return false;
		}
		m_pTileset = g_pTileset;
		return(loadTilemap(tileMapStr, mapPosition, numTiles));
	}

	bool TurretTilemap::loadTilemap(const string &tileMapStr, const Coord2D & mapPosition, const Coord2D &numTiles) {
		m_numTiles = numTiles;
		m_position = mapPosition;

		if(!loadTileMatrix(tileMapStr)) {
			return false;
		}

		for(int i = 0; i < m_numTiles.y; i++) {
			for(int j = 0; j < m_numTiles.x; j++) {
				if((*m_pTilemapMatrix)(i, j) == 1) {
					if(borderlineBottom(i, j) || (*m_pTilemapMatrix)(i + 1, j) == 0) {
						m_tileVector.push_back(createNewTile(i, j, turretBottom, Tileset::turretBottom));
					} else {
						m_tileVector.push_back(createNewTile(i, j, turretBottom, Tileset::turretTopAndBottom));
					}
					m_tileVector.push_back(createNewTile(i, j, turretTop, Tileset::turretTop));

				} else {
					m_tileVector.push_back(createNewTile(i, j, turretBottom, Tileset::noTurret));
					m_tileVector.push_back(createNewTile(i, j, turretTop, Tileset::noTurret));
				}
			}
		}
		return true;
	}
}